package com.itheima.d2_xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileReader;
import java.util.List;

public class JobXmlTest {
    public static void main(String[] args) throws Exception {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new FileReader(
                "D:\\source_code\\workspace\\properties-xml-logback-app\\src\\com\\itheima\\d2_xml\\books.xml"
        ));
        Element root = document.getRootElement();
        List<Element> elements = root.elements();
        elements.forEach(element -> {
            Attribute idAttr = element.attribute("id");
            String id = idAttr.getValue();

            Element titleElement = element.element("title");
            String title = titleElement.getText();

            Element publishedElement = element.element("published");
            String published = publishedElement.getText();

            Element dateElement = element.element("date");
            String date = dateElement.getText();

            Element priceElement = element.element("price");
            String price = priceElement.getText();

            System.out.println(
                    "id: " + id
                            + " title: " + title
                            + " published: " + published
                            + " date: " + date
                            + " price: " + price
            );
        });
    }
}
